import './index.scss';
export default ngModule => {
    ngModule.directive('selfTable', ['$compile', '$timeout', function ($compile, $timeout) {
        return {
            restrict: 'E',
            template: require('./index.html'),
            scope: {
                columns: '=',
                data: '='
            },
            link: function (scope, element, attr) {
                scope.mainCtr = {
                    isCheckedAll: false,
                    checkedAll() {
                        $timeout(() => {
                            _.forEach(scope.data, item => {
                                item.checked = this.isCheckedAll;
                            });
                        });
                    },
                    checkedItem() {
                        $timeout(() => {
                            let flag = true;
                            _.forEach(scope.data, item => {
                                if (!item.checked) {
                                    flag = false;
                                    return false;
                                }
                            });
                            this.isCheckedAll = flag;
                        })
                    },
                    /** 格式化表头*/
                    renderHead() {
                        _.forEach(this.columns, c => {
                            c.formatterName = c.name;
                            if (c.showRender && c.render) {
                                let compileFn = $compile(c.render());
                                let $doms = c.formatterName = compileFn(scope);
                                this.bindEventByListener($doms, c);
                            }
                        })
                    },
                    renderBody() {
                        _.forEach(this.data, d => {
                            _.forEach(this.columns, c => {
                                let { key } = c;
                                if (!d[key]) {
                                    if (c.render) {
                                        let compileFn = $compile(c.render());
                                        let $doms = d[key] = compileFn(scope);
                                        this.bindEventByListener($doms, c, d)
                                    } else if (c.formatter) {
                                        d[key] = c.formatter;
                                    }
                                }
                            })
                        });
                    },
                    /**绑定结点中以ng-开头的事件 */
                    bindEventByListener($doms, c, d) {
                        _.forEach($doms, dom => {
                            let { nodeType, children } = dom;
                            if (nodeType !== 3) {
                                let attrs = dom.getAttributeNames();
                                _.forEach(attrs, a => {
                                    if (/^(ng-)/.test(a)) {
                                        let funcName = dom.getAttribute(a);
                                        scope.funcName = c.ng[funcName];
                                        let eventName = a.slice(3);
                                        dom.addEventListener(eventName, (e) => {
                                            let callback = c.ng[funcName];
                                            callback && typeof callback === 'function' && callback(e, d);
                                        })
                                    }
                                });
                                if (children && children.length) {
                                    this.bindEventByListener(children, c, d);
                                }
                            }
                        })
                    },
                    handleSort(key, type) {
                        console.log(key, type)
                    },
                    init() {
                        this.showCheckbox = scope.columns[0].showCheckbox;
                        this.data = scope.data;
                        this.columns = scope.columns.slice(1);
                        // this.renderHead();
                        this.renderBody();
                    }
                }
                scope.mainCtr.init()
            }
        }
    }]);
    ngModule.directive('cellDiv', ['$compile', function ($compile) {
        return {
            restrict: 'E',
            template: `<div class="cell-div"></div>`,
            scope: {
                render: '='
            },
            replace: true,
            link(scope, element, attr, $transclude) {
                element.html(scope.render);
            }
        }
    }])
}